package com.sskj.gcc.ui.activity;


import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.github.yoojia.inputs.ValueScheme;
import com.github.yoojia.inputs.WidgetAccess;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.sskj.common.base.App;
import com.sskj.common.base.AppManager;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.InputUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.gcc.R;
import com.sskj.gcc.bean.LoginBean;
import com.sskj.gcc.bean.rxbus.AccountBean;
import com.sskj.gcc.component.DaggerUserDataComponent;
import com.sskj.gcc.presenter.LoginActivityPresenter;
import com.sskj.gcc.util.AppTipUtil;
import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.common.Constants;
import com.sskj.gocoin.common.rxbus2.Subscribe;
import com.sskj.gocoin.common.rxbus2.ThreadMode;
import com.sskj.gocoin.v.activity.GoogleLoginActivity;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.TabItem;
import com.sskj.lib.model.room.UserViewModel;

import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

@Route(path = RConfig.APP_LOGIN)//登录
public class LoginActivity extends BaseActivity<LoginActivityPresenter> {
    @BindView(R.id.tvRegister)
    TextView tvRegister;
    @BindView(R.id.etAccount)
    EditText etAccount;
    @BindView(R.id.ivAccountClear)
    ImageView ivAccountClear;
    @BindView(R.id.etPwd)
    EditText etPwd;
    @BindView(R.id.ivPwdToggle)
    ImageView ivPwdToggle;
    @BindView(R.id.btLogin)
    Button btLogin;
    @BindView(R.id.tvPwdForget)
    TextView tvPwdForget;
    //    @BindView(R.id.etCode)
//    EditText etCode;
//    @BindView(R.id.tvGetCheckCode)
//    TextView tvGetCheckCode;
    private AndroidNextInputs inputs;
    private boolean isSee = false;
    @Inject
    UserViewModel userViewModel;
    private MaterialDialog googleCheckDialog;
    @BindView(R.id.commonTabLayout)
    CommonTabLayout commonTabLayout;
    @BindView(R.id.view_login_account)
    View accountView;
    @BindView(R.id.llPwd)
    LinearLayout llPwd;
    //    @BindView(R.id.llCode)
//    LinearLayout llCode;
    private boolean isPwd = true;
    private Disposable timeDis;
    private WidgetAccess access;
    private AndroidNextInputs inputsCheckCode;

    @Override
    public boolean getImmersion() {
        return false;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_login;
    }

    @Override
    public LoginActivityPresenter getPresenter() {
        return new LoginActivityPresenter();
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);
        RxBus.getDefault().register(this);
        ClickUtil.click(tvRegister, () -> {
            ARouter.getInstance().build(RConfig.APP_REGISTER).navigation();
        });

        ClickUtil.click(btLogin, () -> {
            if (isPwd) {
                inputs.clear();
                inputs.add(etAccount, StaticScheme.NotBlank(), InputUtil.Mobile());
            } else {
                inputs.clear();
                inputs.add(etAccount, StaticScheme.NotBlank(), StaticScheme.Email());
            }
            if (inputs.test()) {
                mPresenter.loginPre(etAccount.getText().toString(), isPwd ? etPwd.getText().toString() : etPwd.getText().toString());
            }

        });
        ClickUtil.click(ivAccountClear, () -> {
            etAccount.setText("");
        });
        ClickUtil.click(200, ivPwdToggle, () -> {
            if (isSee) {
                isSee = false;
                etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                ivPwdToggle.setImageResource(R.mipmap.app_icon_hide);
            } else {
                isSee = true;
                etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                ivPwdToggle.setImageResource(R.mipmap.app_icon_show);
            }
        });
        ClickUtil.click(tvPwdForget, () -> {
            ARouter.getInstance().build(RConfig.APP_PWD_FORGET).navigation();
        });

        inputs = new AndroidNextInputs();
        access = new WidgetAccess(this);

        inputsCheckCode = new AndroidNextInputs();


        ArrayList<CustomTabEntity> tabItems = new ArrayList<>();
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.appJavaLoginActivity2), 0, 0));
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.appJavaLoginActivity3), 0, 0));
        commonTabLayout.setTabData(tabItems);
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switch (position) {
                    case 0:
                        if (isPwd)
                            return;
                        changePwd();
                        break;
                    case 1:
                        if (!isPwd)
                            return;
                        changeCode();
                        break;
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    private void changeCode() {
        isPwd = false;
//        llCode.setVisibility(View.VISIBLE);
//        llPwd.setVisibility(View.GONE);
        etPwd.setText("");
        etAccount.setText("");
        accountView.setBackgroundResource(R.mipmap.icon_login_email);
        etAccount.setHint(getString(R.string.app_register_email_hint));
    }

    private void changePwd() {
        isPwd = true;
//        llCode.setVisibility(View.GONE);
//        llPwd.setVisibility(View.VISIBLE);
        etAccount.setText("");
        etPwd.setText("");
        accountView.setBackgroundResource(R.mipmap.icon_login_phone);
        etAccount.setHint(getResources().getString(R.string.appJavaRegisterActivity8));
    }

    public void loginPreSuccess(int type) {
//        RxBus.getDefault().send(Constants.USER_LOGIN);
        if (0 == type) {
            mPresenter.login(etAccount.getText().toString(), etPwd.getText().toString(), "");
        } else {
            googleCheck(etAccount.getText().toString(), etPwd.getText().toString());
        }
    }


    public void loginSuccess(LoginBean data) {
        data.setAccount(etAccount.getText().toString());
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.put(Constants.SP_ACCOUNT, data.getAccount());
        httpHeaders.put(Constants.SP_TOKEN, "Bearer " + data.getToken());
        httpHeaders.put(Constants.LANGUAGE, MyAppLication.getLANGUAGE());
        SPUtil.put(Constants.SP_ACCOUNT, data.getAccount());
        SPUtil.put(Constants.SP_TOKEN, data.getToken());
        SPUtil.put(Constants.SP_MOBILE, etAccount.getText().toString());
        SPUtil.put(Constants.SP_LOGIN_STATUS, true);
        OkGo.getInstance().addCommonHeaders(new HttpHeaders());
        OkGo.getInstance().getCommonHeaders().clear();
        OkGo.getInstance().addCommonHeaders(httpHeaders);
        userViewModel.update();
        ARouter.getInstance().build(RConfig.APP_MAIN).navigation();
        finish();
        overridePendingTransition(R.anim.lib_anim_out, R.anim.lib_anim_back);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateAccount(AccountBean accountBean) {
        if (etAccount != null) {
            etAccount.setText(accountBean.getAccount());
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.lib_anim_out, R.anim.lib_anim_back);
        AppManager.getAppManager().AppExit(this);


    }

    public void googleCheck(String account, String password) {
        if (googleCheckDialog == null) {
            googleCheckDialog = AppTipUtil.showGoogleCheckDialog(this, pwd -> {
                mPresenter.checkGoogle(pwd, account, password);
            });
        } else {
            googleCheckDialog.show();
        }
    }

//    public void sendCheckCodeSuccess() {
//        DisposUtil.close(timeDis);
//        timeDis = TimeUtil.newTime()
//                .subscribe(aLong -> {
//                    if (aLong > 0) {
//                        tvGetCheckCode.setText(aLong + "s");
//                    } else {
//                        tvGetCheckCode.setText(App.INSTANCE.getString(R.string.appJavaRegisterActivity10));
//                        tvGetCheckCode.setEnabled(true);
//                    }
//                });
//    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(timeDis);
        RxBus.getDefault().unregister(this);

        super.onDestroy();
    }

//    public void sendCheckCodeFail() {
//        tvGetCheckCode.setEnabled(true);
//    }

//    public void checkCodeSuccess() {
//        ARouter.getInstance().build(RConfig.APP_REGISTER_NEXT)
//                .withString(Constans.CODE, etCode.getText().toString())
//                .withString(Constans.ACCOUNT, etAccount.getText().toString())
//                .navigation();
//        finish();
//    }
}
